#include "pack_tcp.h"
#include "forward_util.h"
#include "common.h"
#include <stdio.h> // NULL

pack_tcp::pack_tcp(){
	ptr = NULL;
}

pack_tcp::pack_tcp(void* ptr){
	this->ptr = (tcphdr*)ptr;
}

pack_tcp::~pack_tcp(){

}

uint16_t pack_tcp::get_src(){
	return ntohs(ptr->source);
}

uint16_t pack_tcp::get_dst(){
	return ntohs(ptr->dest);
}

void pack_tcp::set_src(uint16_t port){
	ptr->source = htons(port);
}

void pack_tcp::set_dst(uint16_t port){
	ptr->dest = htons(port);
}

uint16_t pack_tcp::get_check(){
	return ntohs(ptr->check);
}

uint32_t pack_tcp::get_len(){
	return sizeof(struct tcphdr);
}

char* pack_tcp::get_app_layer(){
	char* p = (char*)(ptr + 1);
	return p;
}

void pack_tcp::print_buffer(){

	uint16_t source = ntohs(ptr->source);
	uint16_t dest = ntohs(ptr->dest);
	uint32_t seq = ntohl(ptr->seq);
	uint32_t ack_seq = ntohl(ptr->ack_seq);
	uint16_t window = ntohs(ptr->window);
	uint16_t check = ntohs(ptr->check);
	uint16_t urg_ptr = ntohs(ptr->urg_ptr);


	LOGI("[TCP] source=%u dest=%u seq=%u ack_seq=%u window=%u check=%u urg_ptr=%u",
			source,
			dest,
			seq,
			ack_seq,
			window,
			check,
			urg_ptr);


	forward_util::print_buff((char*)ptr, get_len());
}

bool pack_tcp::get_fin(){
	return ptr->fin ? true : false;
}

bool pack_tcp::get_rst(){
	return ptr->rst ? true : false;
}

